      // pages/myOrder/myOrder.js
const app = getApp()
var url = require('../../utils/url.js');
var page = 1;
var last_page = 0;
var type;
var token;
var status;
var _type;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    console.log(page)
    wx.request({
      url: url.URl + 'my/order/list',
      data: { page: page, status: status },
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        that.setData({
          listItem: that.data.listItem.concat(res.data.data.data)
        });
        page++;
      }
    });
  } else {
    wx.hideLoading();
    
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBar: [
      { name: "待付款", id: "0" },
      { name: "待发货", id: "1" },
      { name: "待收货", id: "2" }, 
      { name: "已完成", id: "3" }, 
      { name: "售后", id: "5" }, 
    ],
    activetab: 0,
    listItem: [],
    queS:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that=this;
    if (options.type == 0) {
      that.setData({
        activetab: 0
      })
      status = 0;
    } else if (options.type == 1) {
      that.setData({
        activetab: 1
      })
      status = 1;
    } else if (options.type == 2) {
      that.setData({
        activetab: 2
      })
      status = 2;
    } else if (options.type == 3) {
      that.setData({
        activetab: 3
      })
      status = 3;
    } else if (options.type == 4) {
      that.setData({
        activetab: 4
      })
      status = 5;
    }
  },
  getLIst(){
    var that=this;
    that.setData({
      queS: true,
      listItem: [],
    })
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    if (page > 1) {
      page--;
    }
    that.setData({
      queS: true
    })
    wx.request({
      url: url.URl + 'my/order/list',
      data: { page: page, status:status },
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          that.setData({
            listItem:res.data.data.data
          })
        }else if(res.data.code==201){
          that.setData({
            queS:false
          })
        } else if (res.data.code == 401) {
          
        } else {
         
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  toCencel:function(e){
     var that=this;
     var id = e.currentTarget.dataset.id;
     wx.showModal({
       title: '温馨提示',
       content: '您确定取消订单吗',
       showCancel: true,
       cancelText: '我再想想',
       cancelColor: '',
       confirmText: '确定',
       confirmColor: '#DD2626',
       success: function(res) {
         if(res.confirm){
           wx.showLoading({
             title: '加载中..',
             mask: true,
           })
           wx.request({
             url: url.URl + 'my/order/cancel',
             data: { id:id },
             method: 'post',
             header: {
               'Authorization': token
             },
             success: function (res) {
               console.log(res)
               wx.hideLoading()
               var msg = res.data.msg
               if (res.data.code == 200) {
                 wx.showToast({
                   title: msg,
                   icon: 'none',
                   duration: 2000,
                   success:function(){
                     setTimeout(() => {
                       // 模拟请求数据，并渲染
                       that.setData({
                         listItem: []
                       })
                       that.getLIst()
                       // 数据成功后，停止下拉刷新
                     }, 1000);
                   }
                 })
               } else {
                 wx.showToast({
                   title: msg,
                   icon: 'none',
                   duration: 2000
                 })
               }
             }
           })
         }
       },

     })
  },
  tobuy:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(e)
    console.log(id)
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/order/tobuy',
      data: { id: id },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        if (res.data.code == 201) {

          
        } else {
          wx.requestPayment({
            'appId': res.data.appId,
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            'success': function (res) {
              that.setData({
                disabled: false
              })
              console.log(res)
              wx.navigateTo({
                url: '/pages/success/success?type=1',
                success: function (res) { 
                  that.setData({
                    listItem: []
                  })
                  that.getLIst()
                },
                fail: function (res) { },
                complete: function (res) { },
              })
            },
            fail: function () {


            }
          })
        }
      }
    })
  },
  onChange:function(e){
    var that=this;
    var index = e.detail.index;
    if(index==0){
      status = 0;
      page = 1;
    } if (index == 1) {
      status = 1;
      page = 1;
    } if (index == 2) {
      status = 2;
      page = 1;
    } if (index == 3) {
      status = 3;
      page = 1;
    } if (index == 4) {
      status = 5;
      page = 1;
    }
    that.setData({
      listItem: []
    })
    that.getLIst()
  },
  remind:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(e)
    console.log(id)
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/order/remind',
      data: { id: id },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        wx.showToast({
          title: msg,
          icon: 'none',
        })
      }
    })
  },

  confirm:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(e)
    console.log(id)
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/order/confirm',
      data: { id: id },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        if(res.data.code==200){
          that.setData({
            listItem: []
          })
          that.getLIst()
          wx.showToast({
            title: msg,
            icon: 'none',
          })
        }
      }
    })
  },
  del:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(e)
    console.log(id)
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/order/del',
      data: { id: id },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        that.setData({
          listItem: []
        })
        that.getLIst()
        var msg = res.data.msg
        wx.showToast({
          title: msg,
          icon: 'none',
        })
      }
    })
  },
  returns:function(e){
     var that=this;
     var item=e.currentTarget.dataset.item;
     var jsonItem = JSON.stringify(item);
     wx.navigateTo({
       url: '/pages/myReturn/myReturn?item=' + jsonItem,
       success: function(res) {},
       fail: function(res) {},
       complete: function(res) {},
     })
  },
  evaluation:function(e){
    var that = this;
    var item = e.currentTarget.dataset.item;
    var jsonItem = JSON.stringify(item);
    wx.navigateTo({
      url: '/pages/myevaluation/myevaluation?item=' + jsonItem,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        token = res.data
        that.getLIst(status)
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
     page=1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var self = this;
    setTimeout(() => {
      // 模拟请求数据，并渲染
      page=1;
      self.getLIst()
      // 数据成功后，停止下拉刷新
      wx.stopPullDownRefresh();
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})